#include "PhysicObject.h"
#include <SDL_rotozoom.h>
#include "Core.h"
#include "Manager.h"

PhysicObject::PhysicObject(Image* image, b2Body* body)
:mImage(image)
,mBody(body)
{

    /*b2Vec2 position = mBody->GetPosition();
    const b2XForm& xf = mBody->GetXForm();

    b2PolygonShape* poly = (b2PolygonShape*)mBody->GetShapeList();
    int32 vertexCount = poly->GetVertexCount();
    const b2Vec2* localVertices = poly->GetVertices();

    b2Assert(vertexCount <= b2_maxPolygonVertices);
    b2Vec2 vertices[b2_maxPolygonVertices];

    for (int32 i = 0; i < vertexCount; ++i)
    {
        vertices[i] = b2Mul(xf, localVertices[i]);
    }
    SDL_Rect dstrect;
    dstrect.x = vertices[0].x;
    dstrect.y = vertices[0].y;
    for(int x = 0; x < 4; ++x)
    {
        if(vertices[x].x<dstrect.x)
            dstrect.x = vertices[x].x;
        if(vertices[x].y<dstrect.y)
            dstrect.y = vertices[x].y;
    }*/
    mDstImage = image;

    //mPosition = dstrect;

    //SDL_Surface * img2 = rotozoomSurface (img, body->GetAngle()*-57.2957796, 1, 0);
    //SDL_BlitSurface(img2, 0, screen, &dstrect);
}
void PhysicObject::Update()
{
    b2Vec2 position = mBody->GetPosition();
    const b2XForm& xf = mBody->GetXForm();

    b2PolygonShape* poly = (b2PolygonShape*)mBody->GetShapeList();
    int32 vertexCount = poly->GetVertexCount();
    const b2Vec2* localVertices = poly->GetVertices();

    b2Assert(vertexCount <= b2_maxPolygonVertices);
    b2Vec2 vertices[b2_maxPolygonVertices];

    for (int32 i = 0; i < vertexCount; ++i)
    {
        vertices[i] = b2Mul(xf, localVertices[i]);
    }
    SDL_Rect dstrect;
    dstrect.x = vertices[0].x;
    dstrect.y = vertices[0].y;
    for(int x = 0; x < 4; ++x)
    {
        if(vertices[x].x<dstrect.x)
            dstrect.x = vertices[x].x;
        if(vertices[x].y<dstrect.y)
            dstrect.y = vertices[x].y;
    }

    mPosition = dstrect;
    //SDL_Surface* teste = SDL_LoadBMP("Media\box.bmp");
//    rotozoomSurface (teste, mBody->GetAngle()*-57.2957796, 1, 0);

    //mDstImage->UpdateSurface(rotozoomSurface (mImage->GetSurface(), mBody->GetAngle()*-57.2957796, 1, 0));
    SDL_BlitSurface( mImage->GetSurface(), NULL, Manager::GetSDLScreen(),&dstrect);
}

Image* PhysicObject::GetImage()
{
    return mDstImage;
}
SDL_Rect& PhysicObject::GetSDLRect()
{
    return mPosition;
}

PhysicObject::~PhysicObject()
{
    //dtor
}
